package com.study.samsung.Dao;

import com.study.samsung.Entity.Commodity;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class PaymentDao {
    SQLHelper helper = new SQLHelper();
    public List<Commodity> selectAll(Integer did){
        String sql = "SELECT * FROM commodity a LEFT JOIN detail b ON a.sid = b.sid " +
                "LEFT JOIN commoditycolor c ON b.yid=c.yid " +
                "LEFT JOIN commoditymemory d ON b.nid = d.nid " +
                "LEFT JOIN order_details e ON b.sxid=e.sxid " +
                "LEFT JOIN orders f ON e.did=f.did " +
                "WHERE f.did=?";
        return helper.query(sql,new CommodityRowMapper(),did);
    }

    public Integer update(Integer did,String dstatus){
        String sql = "UPDATE orders SET dstatus=? WHERE did=?";
        return helper.update(sql,dstatus,did);
    }

    public Integer deleteCart(Integer sxid){
        String sql = "DELETE FROM shopping_cart WHERE sxid=?";
        return helper.update(sql,sxid);
    }

    class CommodityRowMapperParent implements RowMapper<Commodity> {
        @Override
        public Commodity map(ResultSet rs) throws SQLException {
            Commodity com=new Commodity();
            com.setSid(rs.getInt("sid"));
            com.setSname(rs.getString("sname"));
            com.setUrl(rs.getString("url"));
            com.setSprice(rs.getBigDecimal("sprice"));
            com.setLid(rs.getInt("lid"));
            return com;
        }
    }
    class CommodityRowMapper extends CommodityRowMapperParent {
        @Override
        public Commodity map(ResultSet rs) throws SQLException {
            Commodity com = super.map(rs);
            com.setSxid(rs.getInt("sxid"));
            com.setXprice(rs.getBigDecimal("xprice"));
            com.setNid(rs.getInt("nid"));
            com.setNname(rs.getString("nname"));
            com.setYname(rs.getString("yname"));
            com.setDnumber(rs.getString("dnumber"));
            com.setDtotal(rs.getInt("dtotal"));
            com.setPay(rs.getString("pay"));
            return  com;
        }
    }

}
